/*
 * @Author: songwh
 * @Date: 2017-06-15 17:54:42
 * @Last Modified by: songwh
 * @Last Modified time: 2017-06-27 10:22:17
 */

import datepicker from 'vue-datepicker'
import { modal } from 'vue-strap'
import extend from 'extend'
import getDatePickerOption from '../../../util/date-picker-option'
import { newOpenAd, showMsg } from '../../../vuex/action'
const getLength = (str) => {
  if (str == null) return 0
  if (typeof str !== 'string'){
    str += ''
  }
  /* eslint-disable */
  return str.replace(/[^\x00-\xff]/g,'01').length
  /* eslint-enable */
}
export default {
  name: 'GaoUpdate',
  props: ['ad', 'position'],
  components: {
    'vue-modal': modal,
    'vue-datepicker': datepicker
  },
  data() {
    return {
      modalStatus: false,
      datePickerOption: getDatePickerOption(),
    }
  },
  computed: {
  },
  vuex: {
    actions: {
      xUpdateAd: newOpenAd,
      xShowMsg: showMsg
    }
  },
  methods: {
    validAd(data) {
      for (let name in data) {
        if ((data[name] + '').length === 0) {
          return '* 标记的均为必填/必选项'
        }
      }
    },
    showModal() {
      this.modalStatus = true
    },
    submit() {
      var formData = new FormData()
      var ad = extend(true, {}, this.ad)
      var errorMsg = this.validAd(ad)
      if (errorMsg) {
        return this.xShowMsg({
          type: 'danger',
          content: errorMsg
        })
      }
      if (getLength(this.ad.securityCode)> 10){
        return this.xShowMsg({
          type: 'danger',
          content: '券商code太长了'
        })
      }
      for (let name in ad) {
        if (name !== 'picFile') {
          formData.append(name, ad[name])
        }
      }
      formData.append('picFile', this.$els.file.files[0])
      this.xUpdateAd(formData)
        .then(() => {
          this.modalStatus = false
        })
        .catch((res) => {
          this.xShowMsg({
            type: 'danger',
            content: res.error_msg
          })
        })
    }
  }
}
